package executeUpdate;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

// 通过 JDBC 修改数据
public class JDBCUpdate {

    public static void main(String[] args) throws SQLException {
        // 1. 创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java107?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1111");

        // 2. 建立连接
        Connection connection = dataSource.getConnection();

        // 3. 构造 SQL 语句
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入将要修改的同学的学号: ");
        String id = scanner.next();
        System.out.println("请输入要修改的同学姓名: ");
        String name = scanner.next();
        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setString(2,id);

        // 4. 执行 SQL
        int n = statement.executeUpdate();
        System.out.println("have "+ n + " row affected");

        // 5. 关闭释放资源
        statement.close();
        connection.close();
    }

}
